#!/usr/bin/python
import random
import cgi, cgitb
import MySQLdb
import os
from config import *
form = cgi.FieldStorage()
configure = config()
def headerPrint():
    print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Original ~ Shorten those long urls</title><link rel="stylesheet" href="styles.css" type="text/css" /></head><body><div id="wrap">	<div class="header">		<div class="title">			<h1><a href="http://origin.al.eu.org">Original</a></h1>			<h2>Python URL Shortening Service</h2>		</div>		<div class="nav">			<ul>				<li><a href="index.html">Home</a></li>			</ul>		</div>		<div class="clear"></div>	</div>	<div class="pagewrapper">		<div class="innerpagewrapper">			<div class="page">				<div class="content">					<p>'
def footerPrint():
    print '</p>				</div>				<div class="footer">					<p>&copy;&nbsp;2010&nbsp;<a href="http://origin.al.eu.org">Origin.al.eu.org</a>|Design: <a href="http://www.spyka.net">spyka webmaster</a></p> 				</div>			</div>		</div>	</div></div></body></html>'
print 'Content-type: text/html\n\n'
headerPrint()
ip = cgi.escape(os.environ["REMOTE_ADDR"])
email = form.getvalue('email')
passwd = form.getvalue('passwd')
if(email==None):
    print "<h2><font color='red'>Access Denied</font></h2>"
    footerPrint()
    exit()
try:
    db = MySQLdb.connect(host=configure.dbHostname,user=configure.dbUsername,passwd=configure.dbPassword,db=configure.dbName)
    sql = """select passwd,apikey from link_database_register where email = '%s'""" %(email)
    db.query(sql)
    result = db.store_result()
except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
result = result.fetch_row()
if(len(result)==0):
    print "<h2><font color='red'>User not found</font></h2>... <a href='./login.html'>go back</a>"
    footerPrint()
    exit()
passwdDB = result[0][0]
apikey = result[0][1]
if(passwd!=passwdDB):
    print "<h2><font color='black'>Incorrect Password</font></h2>"
else:
    print "<h2><font color='black'>Email Id :</font></h2><h2><font color='green'> "+email+"</font></h2>"
    print "<h2><font color='black'>ApiKey :</font></h2><h2><font color='green'> "+apikey+"</font></h2>"
footerPrint()